package com.taiyi.renaissancemall.ui.main.goodsdetail;

import android.content.Intent;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.bean.GoodDetailShareBean;
import com.taiyi.renaissancemall.bean.GoodsDetailBean;
import com.taiyi.renaissancemall.bean.ProductSpecPriceBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class GoodsDetailPresent extends BasePresent<GoodsDetailView> {

    public void getGoodsDetail() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityid", view.getCommodityId());
        HttpUtils.postRequest(BaseUrl.GET_GOODS_DETAIL, this, map, new JsonCallback<BaseResultBean<GoodsDetailBean>>() {
            @Override
            public BaseResultBean<GoodsDetailBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<GoodsDetailBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getGoodsDetailSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getGoodsDetailFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<GoodsDetailBean>> response) {
                super.onError(response);
            }
        });
    }


    public void addShoppingCar() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityspecid", view.getCommoditySpecId());
        map.put("number", view.getProductNum());
        HttpUtils.postRequest(BaseUrl.ADD_SHOPPING_CAR, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                ToastUtils.showLongToast(response.body().msg_cn);
                if (response.body().code == 0) {
                    view.addShopCarSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.addShopCarFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

    public void priceFromSpecId() {
        Map<String, String> map = new HashMap<>();
        map.put("specproids", view.getSpecIds());
        map.put("commodityid", view.getCommodityId());
        HttpUtils.postRequest(BaseUrl.PRICE_FROM_SPEC_ID, this, map, new JsonCallback<BaseResultBean<ProductSpecPriceBean>>() {
            @Override
            public BaseResultBean<ProductSpecPriceBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<ProductSpecPriceBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getSpecIdSuccess(response.body().data);
                } else if (response.body().code == -1) {//登录失效
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getGoodsDetailFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<ProductSpecPriceBean>> response) {
                super.onError(response);
            }
        });
    }

    public void cancelCollection() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityid", view.getCommodityId());
        HttpUtils.postRequest(BaseUrl.CANCEL_COLLECTION, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.cancelCollectionSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.cancelCollectionFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

    public void addCollection() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityid", view.getCommodityId());
        HttpUtils.postRequest(BaseUrl.ADD_COLLECTION, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.collectionSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.collectionFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getShareInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("commodityid", view.getCommodityId());
        HttpUtils.postRequest(BaseUrl.GOOD_SHARE, this, map, new JsonCallback<BaseResultBean<GoodDetailShareBean>>() {
            @Override
            public BaseResultBean<GoodDetailShareBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<GoodDetailShareBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getShareInfoSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getShareInfoFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<GoodDetailShareBean>> response) {
                super.onError(response);
            }
        });
    }

    public void shareGetScore() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.SHARE_GET_SCORE, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.shareGetScoreSuccess();
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.shareGetScoreFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

}
